﻿namespace Furesoft.Styling.Converters
{
    using System.Text.RegularExpressions;

    internal class StringConverter : IConverter<char[]>
    {
        #region Public Methods and Operators

        public override char[] Convert(string s)
        {
            string r = s;

            var p = @"(""|')(?<str>((?:\\.|[^""\\])*))\1";
            if (Regex.IsMatch(r, p))
            {
                r = Regex.Match(s, p).Groups["str"].Value;
            }
            else
            {
                throw new CssException("Syntax Error on string");
            }

            return r.ToCharArray();
        }

        public override string Convert(char[] s)
        {
            return "'" + string.Join("", s) + "'";
        }

        public string ToString(char[] c)
        {
            return string.Join("", c);
        }

        #endregion
    }
}